package juc.piped;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.Scanner;

/*terminal输入另一个线程输出*/
public class SystemIn4ThreadOut {
    public static void main(String[] args) throws IOException {
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader();
        writer.connect(reader);
        new Thread(new Consumer2(reader)).start();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String next = scanner.next();
            writer.write(next);
        }
    }
}


class Consumer2 implements Runnable {
    private PipedReader reader;

    public Consumer2(PipedReader reader) {
        this.reader = reader;
    }

    @Override
    public void run() {
        int recv;
        try {
            while ((recv = reader.read()) != -1) {
                System.out.print((char) recv);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}